package com.ease.map.handler.feature {
    import com.ease.map.Map;
    import com.ease.map.events.FeatureEvent;
    import com.ease.map.feature.Feature;
    import com.ease.map.geometry.basetype.Pixel;
    import com.ease.map.handler.Handler;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    /**
     * Depracated, should be removed in favor of SelectFeaturesHandler
     *
     * This handler has been created in particurlaly for The edition mode
     * It allows the user to differenciate click from double click and drag/drop
     * */
    public class FeatureClickHandler extends Handler {
        private var _featureEvent:FeatureEvent;
        /**
         * We use a timer to detect double click, without throwing a click before.
         */
        private var _timer:Timer = new Timer(250, 1);

        private var _startPixel:Pixel = null;
        /**
         * callback function oneclick(evt:FeatureEvent):void
         */
        private var _click:Function = null;
        /**
         * callback function doubleClick(evt:FeatureEvent):void
         */
        private var _doubleClick:Function = null;

        private var _clickNum:Number = 0;

        private var _isdragging:Boolean = false;

        private var _tolerance:Number = 2;

        private var _ctrl:Boolean = false;

        /**
         * Constructor
         * @param map:Map Map object
         * @param active:Boolean to activate or deactivate the handler
         * */
        public function FeatureClickHandler(map:Map, handlername:String) {
            super(map, handlername);
        }

        override protected function registerListeners():void {
            if (this.map) {
                this.map.addEventListener(FeatureEvent.FEATURE_MOUSEUP, this.mouseUp);
                this.map.addEventListener(FeatureEvent.FEATURE_MOUSEDOWN, this.mouseDown);
            }
            this._timer.addEventListener(TimerEvent.TIMER, chooseClick);

        }

        override protected function unregisterListeners():void {
            this._timer.stop();
            if (this.map) {
                this.map.removeEventListener(FeatureEvent.FEATURE_MOUSEUP, this.mouseUp);
                this.map.removeEventListener(FeatureEvent.FEATURE_MOUSEDOWN, this.mouseDown);
            }
        }

        //This function is used to detect a click
        //This function provided from the old ClickHandler
        private function chooseClick(event:TimerEvent):void {
            if (_clickNum == 1) {
                if (_click != null) {
                    _click(_featureEvent);
                }
            }
            //double click   
            else {
                if (_doubleClick != null) {
                    _doubleClick(_featureEvent);
                }
            }

            _timer.stop();
            _clickNum = 0;
        }

        /**
         * The MouseDown Listener
         */
        protected function mouseDown(evt:FeatureEvent):void {
            var vectorFeature:Feature = evt.feature as Feature;
            if (vectorFeature != null)
                this._startPixel = new Pixel(evt.feature.layer.mouseX, evt.feature.layer.mouseY);
            this._featureEvent = evt;
            this._ctrl = evt.ctrlPressed;
        }

        /**
         * MouseUp Listener
         */
        protected function mouseUp(evt:FeatureEvent):void {
            if (_isdragging) {
                var tmppx:Pixel = new Pixel(evt.feature.layer.mouseX, evt.feature.layer.mouseY);
                //if the mousedown point is close from the mouseUp point it's a click or a double click
                if (Math.abs(tmppx.x - this._startPixel.x) < this._tolerance && Math.abs(tmppx.y - this._startPixel.y) < this._tolerance) {
                    _isdragging = false;
                }
            }
            if (!_isdragging) {
                if (this._startPixel != null) {
                    //dx and dy variables are use to know if there was a drag or a click
                    var vectorFeature:Feature = evt.feature as Feature;
                    var dx:Number = Math.abs(this._startPixel.x - evt.feature.layer.mouseX);
                    var dy:Number = Math.abs(this._startPixel.y - evt.feature.layer.mouseY);
                    if (dx <= this._tolerance && dy <= this._tolerance) {
                        this.featureClick(evt);
                    }
                }
            }

        }

        //In this function we count the click to different the double click from the click
        //This function provided from the old ClickHandler
        private function featureClick(evt:FeatureEvent):void {
            _featureEvent = evt;
            _clickNum++;
            _timer.start()
        }

        //getters && setters
        /**
         * double click callback function
         * */
        public function get doubleclick():Function {
            return this._doubleClick;
        }

        /**
         * @private
         * */
        public function set doubleclick(value:Function):void {
            this._doubleClick = value;
        }

        /**
         *  click callback function
         * */
        public function get click():Function {
            return this._click;
        }

        public function set click(value:Function):void {
            this._click = value;
        }

        public function get tolerance():Number {
            return _tolerance;
        }

        public function set tolerance(value:Number):void {
            _tolerance = value;
        }


    }
}